package com.hsgene.message.service.impl;

import com.hsgene.message.domain.SmsListContentInfo;
import com.hsgene.message.domain.SmsListInfo;
import com.hsgene.message.dto.SmsAddInfoDto;
import com.hsgene.message.dto.SmsManagerConditionDto;
import com.hsgene.message.persistence.SmsManagerRepository;
import com.hsgene.message.service.SmsManagerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 消息中心service
 * @author wxf
 * @date 2018/11/14 15:46
 **/
@Service
public class SmsManagerServiceImpl implements SmsManagerService {

    @Resource
    private SmsManagerRepository smsManagerRepository;

    /**
     * 获取列表信息
     *
     * @param smsDto 条件
     * @return
     */
    @Override
    public SmsListInfo getSmsList(SmsManagerConditionDto smsDto) {
        Integer size = smsDto.getSize();
        smsDto.setPage((smsDto.getPage() - 1) * size);
        //获取列表数据
        List<SmsListContentInfo> contentInfos = smsManagerRepository.getSmsList(smsDto);
        //计算sumcount、totalpage
        Integer sumCount = smsManagerRepository.getSmsListCount(smsDto);
        SmsListInfo info = new SmsListInfo();
        info.setInfos(contentInfos);
        if (sumCount == null) {
            info.setSumCount(0);
            info.setTotalPage(0);
            return info;
        }
        info.setSumCount(sumCount);
        info.setTotalPage(sumCount % size == 0 ? sumCount / size : sumCount / size + 1);
        return info;
    }

    /**
     * 校验是否处理
     * @param smsId 消息ID
     * @return
     */
    @Override
    public Integer checkDealById(String smsId) {
        return smsManagerRepository.checkDealById(smsId);
    }

    /**
     * 添加消息
     * @param addInfoDto 消息信息
     * @param createTime 创建时间
     * @return
     */
    @Override
    public Boolean addSms(SmsAddInfoDto addInfoDto, Date createTime) {
        return smsManagerRepository.addSms(addInfoDto,createTime);
    }

    /**
     * 处理更新状态
     * @param smsId 消息ID
     * @return
     */
    @Override
    public Boolean updateDealInfo(String smsId) {
        return smsManagerRepository.updateDealInfo(smsId);
    }
}
